<style lang="css" scoped>
.el-table .cell .el-tooltip {
  white-space: pre-line;
}
</style>

<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="推送消息名称:" label-width="120px">
              <el-input
                clearable
                v-model="where.name"
                placeholder="请输入推送消息名称"
              />
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
                v-if="permission.includes('sys:jiguang-push:index')"
                >查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:jiguang-push:add')"
            >添加
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="
              permission.includes('sys:jiguang-push:edit') && row.is_push === 0
            "
            >修改
          </el-link>
          <el-link
            type="warning"
            :underline="false"
            icon="el-icon-s-promotion"
            @click="pushData(row)"
            v-if="
              permission.includes('sys:jiguang-push:push') && row.is_push === 0
            "
            >推送
          </el-link>
          <el-link
            type="success"
            :underline="false"
            icon="el-icon-view"
            @click="openView(row)"
            v-if="
              permission.includes('sys:jiguang-push:push') &&
              row.push_result !== 1
            "
            >查询推送结果
          </el-link>
          <el-link
            type="info"
            :underline="false"
            icon="el-icon-document-delete"
            @click="deleteView(row)"
            v-if="
              permission.includes('sys:jiguang-push:push') &&
              row.push_revoked === 0 &&
              row.is_push === 1
            "
            >撤销推送
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此推送消息吗？"
            @confirm="remove(row)"
          >
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:jiguang-push:delete')"
              >删除
            </el-link>
          </el-popconfirm>
        </template>

        <!-- 状态列 -->
        <template slot="status" slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            @change="status(row)"
            :active-value="1"
            :inactive-value="2"
          />
        </template>
        <!-- 搜索场景 -->
        <template slot="push_target" slot-scope="{ row }">
          <el-tag v-if="row.push_target === 1" type="success" size="small"
            >广播所有人
          </el-tag>
          <el-tag v-if="row.push_target === 2" type="error" size="small"
            >设备标签(Tag)
          </el-tag>
          <el-tag v-if="row.push_target === 3" type="primary" size="small"
            >设备别名(Alias)
          </el-tag>
          <el-tag v-if="row.push_target === 4" type="warning" size="small"
            >Registration ID
          </el-tag>
          <el-tag v-if="row.push_target === 5" type="info" size="small"
            >用户分群推送
          </el-tag>
        </template>
        <template slot="push_type" slot-scope="{ row }">
          <el-tag v-if="row.push_type === 1" type="success" size="small"
            >立即发送
          </el-tag>
          <el-tag v-if="row.push_type === 2" type="primary" size="small"
            >定时发送
          </el-tag>
          <el-tag v-if="row.push_type === 3" type="error" size="small"
            >重复发送
          </el-tag>
          <el-tag v-if="row.push_type === 4" type="warning" size="small"
            >围栏发送
          </el-tag>
          <el-tag v-if="row.push_type === 5" type="info" size="small"
            >行为触发
          </el-tag>
        </template>
        <template slot="target_event" slot-scope="{ row }">
          <el-tag v-if="row.target_event === 0" type="success" size="small"
            >默认值
          </el-tag>
          <el-tag v-if="row.target_event === 1" type="success" size="small"
            >应用切换至前台发送
          </el-tag>
          <el-tag v-if="row.target_event === 2" type="primary" size="small"
            >目标切换至后台
          </el-tag>
        </template>
        <template slot="is_push" slot-scope="{ row }">
          <el-tag v-if="row.is_push === 0" type="info" size="small"
            >未推送
          </el-tag>
          <el-tag v-if="row.is_push === 1" type="success" size="small"
            >已推送
          </el-tag>
        </template>
        <template slot="platform" slot-scope="{ row }">
          <el-tag v-if="row.platform === 1" type="success" size="small">
            全部
          </el-tag>
          <el-tag v-if="row.platform === 2" type="primary" size="small"
            >安卓
          </el-tag>
          <el-tag v-if="row.platform === 3" type="warning" size="small"
            >IOS
          </el-tag>
        </template>
        <template slot="push_result" slot-scope="{ row }">
          <el-tag v-if="row.push_result === 0" type="info" size="small">
            未查询推送结果
          </el-tag>
          <el-tag v-if="row.push_result === 1" type="success" size="small"
            >推送成功
          </el-tag>
          <el-tag v-if="row.push_result === -1" type="error" size="small"
            >推送失败
          </el-tag>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <Edit :data="current" :visible.sync="showEdit" @done="reload" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Edit from "./edit.vue";
import { deviceArr, popSceneArr } from "../../format-store";

export default {
  name: "ExamBoxAd",
  components: { Edit },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: "/jiguang.BoxThirdPushMessage/index",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          minWidth: 45,
          align: "center",
          fixed: "left",
        },
        {
          prop: "id",
          label: "ID",
          minWidth: 60,
          align: "center",
          showOverflowTooltip: true,
          fixed: "left",
        },

        {
          prop: "name",
          label: "名称",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
        },
        {
          prop: "is_push",
          label: "是否推送",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          slot: "is_push",
        },
        {
          prop: "push_result",
          label: "推送结果",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          slot: "push_result",
        },
        {
          prop: "push_title",
          label: "推送标题",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
        },
        {
          prop: "push_content",
          label: "推送内容",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
        },
        {
          prop: "platform",
          label: "推送平台",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          slot: "platform",
        },
        {
          prop: "push_target",
          label: "推送的目标人群",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          slot: "push_target",
        },
        {
          prop: "push_type",
          label: "发送时机",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          slot: "push_type",
        },
        {
          prop: "push_time",
          label: "发送时间",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          formatter: (row, column, cellValue) => {
            if (row.push_type === 2) {
              return cellValue;
            } else {
              return "无需定时";
            }
          },
        },
        {
          prop: "target_event",
          label: "目标转化事件",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          slot: "target_event",
        },
        {
          prop: "status",
          label: "是否启用",
          align: "center",
          minWidth: 100,
          resizable: false,
          slot: "status",
        },
        {
          prop: "create_time",
          label: "创建时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
        },
        {
          prop: "update_time",
          label: "更新时间",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center",
        },
        {
          columnKey: "action",
          label: "操作",
          minWidth: 260,
          align: "center",
          resizable: false,
          slot: "action",
          fixed: "right",
        },
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      popSceneArr,
      deviceArr,
      dialogVisible: false,
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = { show_type: 3 };
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .post("/jiguang.BoxThirdPushMessage/delete", { id: row.id })
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的广告吗?", "警告", {
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          this.$http
            .post("boxAd/delete", { id: this.selection.map((d) => d.id) })
            .then((res) => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 更改状态 */
    status(row) {
      const loading = this.$loading({ lock: true });
      let params = Object.assign({
        id: row.id,
        status: row.status,
      });
      this.$http
        .post("/jiguang.BoxThirdPushMessage/status", params)
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
          } else {
            row.status = !row.status ? 1 : 2;
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },

    pushData(row) {
      this.$confirm("确定要推送此条消息吗?", "警告", {
        type: "warning",
      }).then(() => {
        const loading = this.$loading({ lock: true });
        this.$http
          .post("/jiguang.BoxThirdPushMessage/pushMsg", { params: row })
          .then((res) => {
            loading.close();
            if (res.data.code === 0) {
              this.$notify.success(res.data.msg);
              this.reload();
            } else {
              this.$notify.error(res.data.msg);
            }
          })
          .catch((e) => {
            loading.close();
            this.$notify.error(e.message);
          });
      });
    },
    openView(row) {
      this.$confirm("确定要查询此条消息吗?", "提示", {
        type: "warning",
      }).then(() => {
        const loading = this.$loading({ lock: true });
        this.$http
          .post("/jiguang.BoxThirdPushMessage/validate_push_api", {
            params: row,
          })
          .then((res) => {
            loading.close();
            if (res.data.code === 0) {
              this.$notify.success(res.data.msg);
              this.reload();
            } else {
              this.$notify.error(res.data.msg);
            }
          })
          .catch((e) => {
            loading.close();
            this.$notify.error(e.message);
          });
      });
    },
    deleteView(row) {
      this.$confirm("确定要撤销此条消息吗?", "警告", {
        type: "warning",
      }).then(() => {
        const loading = this.$loading({ lock: true });
        this.$http
          .post("/jiguang.BoxThirdPushMessage/delete_push_api", {
            params: row,
          })
          .then((res) => {
            loading.close();
            if (res.data.code === 0) {
              this.$notify.success(res.data.msg);
              this.reload();
            } else {
              this.$notify.error(res.data.msg);
            }
          })
          .catch((e) => {
            loading.close();
            this.$notify.error(e.message);
          });
      });
    },
  },
};
</script>

<style scoped></style>
